import 'package:ac/common/style.dart';
import 'package:ac/data/ac_user_manager.dart';
import 'package:ac/route/ac_route_pages.dart';
import 'package:ac/widget/ac_app_bar.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../model/ac_blogger_model.dart' as blogger;
import '../../model/ac_login_model.dart';
import 'ac_my_posts_logic.dart';

class AcMyPostsPage extends StatelessWidget {
  final logic = Get.find<AcMyPostsLogic>();
  final state = Get.find<AcMyPostsLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AcAppbar(title: "My Posts"),
        body: Column(
          children: [
            Expanded(
              child: Obx(() {
                return GridView.builder(
                  shrinkWrap: true,
                  padding:
                      EdgeInsets.symmetric(horizontal: 20.w, vertical: 12.h),
                  physics: const NeverScrollableScrollPhysics(),
                  itemBuilder: (context, index) {
                    var userInfo = AcUserManager.userInfoModel?.userInfo;
                    var post = logic.postList[index];
                    return _postItem(post, userInfo);
                  },
                  itemCount: logic.postList.toList().length,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    mainAxisSpacing: 16.w,
                    crossAxisSpacing: 11.w,
                    childAspectRatio: 0.7,
                  ),
                );
              }),
            ),
          ],
        ));
  }

  Widget _postItem(blogger.Post item, Userinfo? userInfo) {
    return GestureDetector(
      onTap: () {
        Userinfo userinfo = Userinfo(
          userId: userInfo?.userId,
          nickname: userInfo?.nickname,
          avatar: userInfo?.avatarThumbUrl,
          isFriend: true,
        );
        Get.toNamed(AcRouteNames.postDetail, arguments: {
          "post": item,
          "userInfo": userinfo,
          "isMyPost": true,
        });
      },
      child: Container(
        decoration: BoxDecoration(
          border: Border.all(
            color: const Color(0xFFCCCCCC),
            width: 0.5.w,
          ),
          borderRadius: BorderRadius.circular(8.r),
        ),
        child: Column(
          children: [
            Expanded(
              child: Container(
                width: double.infinity,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(8.r),
                    topRight: Radius.circular(8.r),
                  ),
                  image: DecorationImage(
                    image: AssetImage(item.mainImage),
                    fit: BoxFit.cover,
                  ),
                ),
                alignment: Alignment.topRight,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    GestureDetector(
                      onTap: () {
                        logic.deletePost(item);
                      },
                      child: Image.asset(
                        "images/icon_post_delete.png",
                        width: 30.w,
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Container(
                padding: EdgeInsets.all(10.w),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(8.r),
                      bottomRight: Radius.circular(8.r)),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      item.title.fixAutoLines(),
                      maxLines: 1,
                      style: TextStyle(
                        fontSize: 10.sp,
                        color: textColorBlack,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    SizedBox(height: 8.h),
                    Row(
                      children: [
                        GestureDetector(
                          onTap: () {
                            if(userInfo?.userId == AcUserManager.userInfoModel?.userInfo.userId) return;
                            Get.toNamed(
                              AcRouteNames.userDetail,
                              arguments: userInfo?.userId,
                            );
                          },
                          child: ClipOval(
                            child: CachedNetworkImage(
                              imageUrl: userInfo?.avatarThumbUrl ?? "",
                              width: 20.w,
                              height: 20.w,
                            ),
                          ),
                        ),
                        SizedBox(width: 4.w),
                        Expanded(
                          child: Text(
                            (userInfo?.nickname ?? "").fixAutoLines(),
                            maxLines: 1,
                            style: TextStyle(
                              fontSize: 10.sp,
                              color: textColorBlack,
                              overflow: TextOverflow.ellipsis,
                            ),
                          ),
                        ),
                        SizedBox(width: 8.w),
                        Image.asset(
                          "images/icon_home_${item.isLiked ? "yes" : "no"}.png",
                          width: 15.w,
                          height: 15.w,
                        ),
                      ],
                    )
                  ],
                ))
          ],
        ),
      ),
    );
  }
}
